package cn.tedy.spring.boot.quartz.config;

import cn.tedy.spring.boot.quartz.job.JobDemo;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {
    //定义JobDetail，交给Spring容器
    @Bean
    public JobDetail showTime(){
        System.out.println("showTime方法运行了");
        return JobBuilder.newJob(JobDemo.class)
                .withIdentity("dateTimeJob")
                .storeDurably()
                .build();
    }
    //定义Trigger，交给Spring容器
    @Bean
    public Trigger showTimeTrigger(){
        System.out.println("showTimeTrigger方法运行了");
        //定义cron表达式
        CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("* * 21 12 9 ?");
        return TriggerBuilder.newTrigger()
                .forJob(showTime())//绑定到了JobDetail对象
                .withIdentity("dateTimeTrigger")
                .withSchedule(cron)
                .build();
    }
}
